<template>
  <div class="supplier-check setting-content">
    <h2 class="setting-header">
      <strong>
        <span>计划管理</span>
      </strong>
    </h2>
    <el-tabs v-model="queryData.state" class="demo-tabs" @tab-change="handleTabChange">
      <el-tab-pane :label="item.label" :name="item.value" v-for="(item, index) in tabsOptions" :key="index"></el-tab-pane>
    </el-tabs>
    <div class="top-btns">
      <el-button type="primary" style="margin: 10px 0" @click="handleGoDetails(null,1)">申请计划</el-button>
      <SearchCmp style="margin: 10px 0;" v-if="searchCmpArray.length" :searchCmpArray="searchCmpArray" :searchForm="queryData" @onSubmit="handleSearch" />
    </div>


    <div class="list-container">
      <RuifoxTable :loading="tableLoading" :default-index="true" :default-checkbox="true" v-model:tableData="tableData" v-model:fieldList="fieldList">
        <!-- 多选 -->
        <template #default-checkbox="{element}">
          <div class="table-div-check">
            <el-checkbox class="unmover" v-model="element.checked" @change="handleCheckTable(element.id)" size />
          </div>
        </template>
        <template #state="{element}">
          <el-tag type="info" v-if="element.state == 0">计划暂存</el-tag>
          <el-tag type="warning" v-if="element.state == 1">待审核</el-tag>
          <el-tag type="success" v-if="element.state == 2">申请成功</el-tag>
          <el-tag type="danger" v-if="element.state == 3">已驳回</el-tag>
          <el-tag type="info" v-if="element.state == 4">已作废</el-tag>
        </template>
        <template #procurementType="{element}">
          <span>{{procurementTypeOptions.find(item => item.value == element.procurementType)?.label || '--'}}</span>
        </template>
        <template #options="{element}">
          <div class="table-td-option unmover">
            <el-button type="primary" link @click="handleGoDetails(element.id, 1)">查看</el-button>
            <el-button type="primary" v-if="element.state == 1" link @click="handleGoDetails(element.id, 2)">审核</el-button>
            <el-button type="primary" v-if="element.state == 2" link @click="handleGoProject(element.id)">项目列表</el-button>
            <el-button type="primary" v-if="![0, 4].includes(element.state)" link @click="handleEdit('state', 4, element)">作废</el-button>
            <el-button type="primary" v-if="element.state == 4" link @click="handleEdit('state', 0, element)">恢复</el-button>
          </div>
        </template>
      </RuifoxTable>
      <PaginationView :total="queryData.total" :pageSize="queryData.pageSize" :currentPage="queryData.currentPage" :allChoose="isAllChooseBtn" :pageChoose="isPageChooseBtn" :checkedNum="checkedNum" @changeSize="handleSizeChange" @currentChange="handleCurrentChange" @changePageChoose="handlePageChoose" @changeAllChoose="handleAllChoose" @invertChoose="handleInvertChoose" @refreshList="getTableData" />
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { biddingProcurementList, updatePlanState } from '@/api/bidding'
// import { fileSecretKey } from '@/views/formSystem/formDesign/config/formCmpConfig.js'
import { tableList as mixTable } from '@ruifox-cms/utils'
import { useRouter } from 'vue-router'
// import { ElMessageBox, ElMessage } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
const {
  queryData,
  tableData,
  tableLoading,
  checkedNum,
  isAllChooseBtn,
  isPageChooseBtn,
  getTableData,
  handleSearch,
  handleCurrentChange,
  handleSizeChange,
  handlePageChoose,
  handleAllChoose,
  handleInvertChoose,
  handleCheckTable,
} = mixTable({
  getListApi: biddingProcurementList,
  batchFuncTip: '是否确定删除选中项？',
})
onMounted(() => {
  handleSearch()
})
const router = useRouter()
const defaultQueryData = {
  keyWords: '',
  state: -1,
}
Object.assign(queryData, structuredClone(defaultQueryData))
const handleTabChange = () => {
  setTimeout(() => {
    handleSearch()
  }, 0)
}
const procurementTypeOptions = ref([
  { label: '服务性采购', value: 1 },
  { label: '项目性采购', value: 2 },
  { label: '一般用品采购', value: 3 },
])
const tabsOptions = ref([
  {
    label: '全部',
    value: -1,
  },
  {
    label: '待审核',
    value: 1,
  },
  {
    label: '已通过',
    value: 2,
  },
  {
    label: '未通过',
    value: 3,
  },
  {
    label: '已作废',
    value: 4,
  },
  {
    label: '暂存',
    value: 0,
  },
])
const fieldList = ref([
  {
    title: '采购计划编码',
    name: 'number',
  },
  {
    title: '采购需求计划标题',
    name: 'name',
  },
  {
    title: '采购类型',
    name: 'type',
  },
  {
    title: '状态',
    name: 'state',
  },
  {
    title: '申请人',
    name: 'userName',
  },
  {
    title: '申请时间',
    name: 'createTime',
  },
  {
    title: '操作',
    width: 180,
    fixed: 'right',
    name: 'options',
  },
])

const searchCmpArray = ref([{ label: '关键词', key: 'keyWords' }])
// 详情; pageType 1 查看 2 审核
const handleGoDetails = (id, pageType) => {
  router.push({
    path: '/bidding/project/planDetails',
    query: {
      id,
      pageType,
    },
  })
}
// 项目列表
const handleGoProject = (id) => {
  router.push({
    path: '/bidding/project/project',
    query: {
      id,
    },
  })
}

const handleEdit = (field, val, row) => {
  ElMessageBox.confirm(`<div><p style="font-size: 16px;margin-bottom: 15px;">是否确认恢复？恢复后将进入暂存状态</p></div>`, '操作确认', {
    dangerouslyUseHTMLString: true,
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  }).then(async () => {
    let res = await updatePlanState({ id: row.id, state: val })
    handleSearch()
    if (res.code === 20000) {
      ElMessage({
        message: '操作成功',
        type: 'success',
      })
    }
  })
}
</script>
<style lang="scss" scoped>
.top-btns {
  display: flex;
  align-items: flex-start;
  gap: 0 10px;
}
header {
  margin-bottom: 20px;
  overflow: hidden;
  .left-btn {
    float: left;
    display: flex;
    align-items: center;
  }
  .right-btn {
    float: right;
    display: flex;
    align-items: center;
    .keyWords-search {
      width: 320px;
      margin-right: 20px;
      i {
        cursor: pointer;
        color: $mainColor;
      }
    }
    .el-btn2 {
      .search-icon {
        color: $mainColor;
      }
      span {
        margin-left: 5px;
        color: $black;
      }
      .active-focus {
        color: $mainColor;
      }
    }
    .el-btn1 {
      width: 111px;
      height: 32px;
      .tuo-icon {
        color: $mainColor;
        font-size: 16px;
      }
      span {
        margin-left: 5px;
        color: $black;
      }
      .active-focus {
        color: $mainColor;
      }
    }
    .change-card {
      height: 32px;
      width: 64px;
      box-sizing: border-box;
      border: var(--el-border);
      border-radius: 4px;
      display: flex;
      overflow: hidden;
      margin-left: 20px;
      a {
        display: flex;
        width: 50%;
        height: 100%;
        align-items: center;
        justify-content: center;
      }
      a.active {
        background-color: $mainColor;
        i {
          color: #fff;
        }
      }
    }
  }
}
.ruifox-table tbody tr {
  cursor: auto;
  td {
    p {
      @include textEllipsis(16px, 1);
    }
    a {
      color: $mainColor;
      @include textEllipsis(16px, 1);
      &:hover {
        text-decoration: underline;
      }
    }
  }
}
</style>